package com.example.cachedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.Toast.*;

public class MainActivity extends AppCompatActivity {
    // 图片缓存
    private BitmapLoader bitmapLoader = BitmapLoader.getInstance();
    // 测试url，网上随便选的
    final String imageUrl = "https://img.ithome.com/newsuploadfiles/2014/12/20141223_115629_592.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onPause() {
        super.onPause();
        bitmapLoader.flush();
    }

    // 获取测试图片
    public void getImageButtonClicked(View view) {
        bitmapLoader.loadBitmap(imageUrl, new BitmapLoader.BitmapLoaderCallback() {
            @Override
            public void success(Bitmap bitmap) {
                ImageView getButtonImageView = (ImageView)findViewById(R.id.img_test);
                getButtonImageView.setImageBitmap(bitmap);
            }

            @Override
            public void failure(String message) {
                Toast.makeText(CacheDemoApplication.getContext(), message, LENGTH_LONG).show();
            }
        });
    }

    // 获取缓存大小信息
    public void getSizeButtonClicked(View view) {
        String sizeInfo = bitmapLoader.size();
        if (sizeInfo != null) {
            TextView sizeTextView = (TextView)findViewById(R.id.text_size);
            sizeTextView.setText(sizeInfo);
        }
    }

    // 移除测试图片
    public void removeTestImageButtonClicked(View view) {
        bitmapLoader.removeImage(imageUrl);
    }

    // 跳转下一页
    public void nextButtonClicked(View view) {
        Intent intent = new Intent(this, PhotoActivity.class);
        startActivity(intent);
    }

    public void clearButtonClicked(View view) {
        bitmapLoader.clear();
    }
}
